package com.canlihao.ui.base;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.canlihao.ui.BuildConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import okhttp3.OkHttpClient;

/*******
 *
 *Created by panda李
 *
 *创建时间 2020/2/7
 *
 *类描述：
 *
 ********/
public class BaseApplication extends Application {
    private static final String APP_ID = "wx109b85bf60560123";

    // IWXAPI 是第三方app和微信通信的openApi接口
    public static IWXAPI api;
    public static IWXAPI getWxApi(){
        return BaseApplication.api;
    }
    public OkHttpClient okHttpClient;
    public static BaseApplication myApplication;
    private static Context context;
    public static Context getAppContext() {
        return BaseApplication.context;
    }
    public static BaseApplication getInstance() {
        return myApplication;
    }
    private void regToWx() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, true);

        // 将应用的appId注册到微信
        api.registerApp(APP_ID);

        //建议动态监听微信启动广播进行注册到微信
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                // 将该app注册到微信
                api.registerApp(APP_ID);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));

    }
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
        regToWx();
//        okHttpClient = new OkHttpClient()
//                .newBuilder()
//                .connectTimeout(5, TimeUnit.SECONDS)
//                .readTimeout(5, TimeUnit.SECONDS).build();
        myApplication = this;
        initComponent();
        MultiDex.install(this);
        BaseApplication.context = getApplicationContext();
    }
    void initComponent(){
        if (BuildConfig.DEBUG) {           // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
        ARouter.init(this); // 尽可能早，推荐在Application中初始化
    }
}
